﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using UIDesigner.DesignContainers.Bases;
using System.Diagnostics;
using UIDesigner.Editors;

namespace UIDesigner.SurfaceProperties
{
    public class SurfaceObjectLevelDescriptor : PropertyDescriptor
    {
        private AFDesignerHost m_objHost;
        private IBaseObjectControl m_objControl;
        private SurfaceObjectLevelProperty m_eProperty;
        private bool m_bRemoveSurfaceControls;
        private SurfacePropertiesBase m_objSurrogateProperties;

        public SurfaceObjectLevelDescriptor(AFDesignerHost objHost, IBaseObjectControl objControl, SurfaceObjectLevelProperty eProperty, Attribute[] rgAttributes) : base(Enum.GetName(typeof(SurfaceObjectLevelProperty), eProperty), rgAttributes)
		{
            m_objHost = objHost;
            m_eProperty = eProperty;
            m_objControl = objControl;
		}

        public SurfaceObjectLevelDescriptor(AFDesignerHost objHost, IBaseObjectControl objControl, SurfaceObjectLevelProperty eProperty, Attribute[] rgAttributes, SurfacePropertiesBase objSurrogateProperties) : base(Enum.GetName(typeof(SurfaceObjectLevelProperty), eProperty), rgAttributes)
		{
            m_objHost = objHost;
            m_eProperty = eProperty;
            m_objControl = objControl;
            m_objSurrogateProperties = objSurrogateProperties;
		}

        private SurfacePropertiesBase SurfaceProperties
        {
            get
            {
                if (m_objSurrogateProperties == null)
                {
                    return m_objHost.SurfacePropertiesSet[m_objControl];
                }
                else
                {
                    return m_objSurrogateProperties;
                }
            }
        }

        public override bool CanResetValue(object component)
        {
            if (m_eProperty == SurfaceObjectLevelProperty.CancelOptions)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public override Type ComponentType
        {
            get
            {
                return m_objHost.RootComponent.GetType();
            }
        }

        public override object GetValue(object component)
        {
            if (m_eProperty == SurfaceObjectLevelProperty.CancelOptions)
            {
                return this.SurfaceProperties.CancelOptions;
            }
            else if (m_eProperty == SurfaceObjectLevelProperty.ApplySelection)
            {
                return SavedSurfaceProperties.AppySelectedText; 
            }
            else
            {
                Debugger.Break();
                return null;
            }
        }

        public override bool IsReadOnly
        {
            get
            {
                if (m_eProperty == SurfaceObjectLevelProperty.CancelOptions)
                {
                    return false;
                }
                else if (m_eProperty == SurfaceObjectLevelProperty.ApplySelection)
                {
                    return true;
                }
                else
                {
                    Debugger.Break();
                    return true;
                }
            }
        }

        public override Type PropertyType
        {
            get
            {
                if (m_eProperty == SurfaceObjectLevelProperty.CancelOptions)
                {
                    return typeof(CancelOptions);

                }
                else if (m_eProperty == SurfaceObjectLevelProperty.ApplySelection)
                {
                    return typeof(string);
                }
                else
                {
                    Debugger.Break();
                    return null;
                }
            }
        }

        public override void ResetValue(object component)
        {
            if (m_eProperty == SurfaceObjectLevelProperty.CancelOptions)
            {
                this.SurfaceProperties.CancelOptions = CancelOptions.None;
            }
            else
            {
                Debugger.Break(); 
            }
        }

        public override void SetValue(object component, object value)
        {
            if (m_eProperty == SurfaceObjectLevelProperty.CancelOptions)
            {
                this.SurfaceProperties.CancelOptions = (CancelOptions)value;
            }
            else
            {
                m_bRemoveSurfaceControls = (bool)value;
                m_objControl.RemoveSurfaceControls();
            }
        }

        public override bool ShouldSerializeValue(object component)
        {
            return false;
        }

        public override string Category
        {
            get
            {
                return "Object Level Properties";
            }
        }

        public override string DesignComments
        {
            get
            {
                return "CancelOption";
            }
        }
    }
}
